<?php

class AdminController extends PanelController {
    
    public $metaKeywords = 'Panel Administratora';
    public $metaDescription = 'Panel Administratora';
    public $pageTitle = 'Panel Administratora';
    
    public $menu = array(
        array(
            'name' => 'Lista ofert kont',
            'link' => 'admin/accountlist',
        ),
        array(
            'name' => 'Dodaj pracownika',
            'link' => 'admin/createworker',
        ),
        array(
            'name' => 'Lista pracowników',
            'link' => 'admin/workerlist',
        ),
        array(
            'name' => 'Sztuczny cron',
            'link' => 'admin/cron',
        ),
        array(
            'name' => 'Zmiana hasła',
            'link' => 'admin/changepassword',
        )
    );
    
    
    public function actionIndex() {
        
        $this->render('index');
    }
    
    public function actionTransfer() {
        $model = new TransferForm();


        if (isset($_POST['TransferForm'])) {
            $model->attributes = $_POST['TransferForm'];

            if ($model->validate()) {
                if ($model->addClient() !== 1) {
                    $model->addError('title', 'Nie udało się wykonać przelew.');
                } else {
                    $this->redirect(array('client/index'));
                }
            }
        }
        $this->render('transfer', array('model' => $model));
    }
    
    public function actionChangePassword(){
        $model = new ChangePasswordForm();
        
        
        if(isset($_POST['ChangePasswordForm']))
        {
            $model->attributes = $_POST['ChangePasswordForm'];
            
            if($model->validate()){
                if($model->changePassword(Yii::app()->user->id, 1) !== 1){
                    $model->addError('oldpassword','Nie udało się zmienić hasła.');
                }else{
                    $this->redirect('admin/info');
                }
            }
        }

        $this->render('application.views.components.change_password', array('model' => $model));
    }
    
    public function actionCron() {
        $this->render('cron');
    }
    
    public function actionCronPay() {
       // Yii::app()->db->createCommand("")->execute();
        $this->redirect(array('admin/info'));
    }
    
    public function actionCronCost() {
       // Yii::app()->db->createCommand("")->execute();
        $this->redirect(array('admin/info'));
    }
    
    public function actionOutsideTransfer(){
        
        $model = new TransferForm('outside');
        
        if (isset($_POST['TransferForm'])) {
            $model->attributes = $_POST['TransferForm'];

            if ($model->validate()) {
                $result = $model->addOutTransfer();
                if ($result === 0) {
                    $this->redirect(array('admin/index'));
                } else{
                    $model->addError('title', 'Nie udało się wykonać przelewu.');
                }
            }
        }
        
        $cashes = Yii::app()->db->createCommand()
                    ->select('')
                    ->from('waluty')
                    ->queryAll();
        
        $this->render('outsideTransfer', array('model' => $model, 'cashes'=> $cashes));
    }
    
    public function actionCreateAccountKind() {
        
        $model = new AccountKindForm();
        
        if(isset($_POST['AccountKindForm']))
        {
            $model->attributes = $_POST['AccountKindForm'];
            
            if($model->validate()){
               if($model->addAccount() > 0){
                   $this->redirect(array('admin/info'));
               }else{
                   $model->addError('name','Nie udało się dodać rodzaju konta.');
               }
            }
        }
        
        $this->render('account_kind', array('model' => $model));
    }
    
    public function actionEditAccountKind() {
        
        if(isset($_GET['id'])){
            $account = Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('rodzaj_konta')
                    ->where('id_rodzaj_konta = '.$_GET['id'])
                    ->queryRow();
            
            if($account['aktywny'] == true){
                Yii::app()->db->createCommand("Update rodzaj_konta set aktywny = NOT COALESCE(aktywny,'t') WHERE id_rodzaj_konta = ".$_GET['id'])->execute();
            }else{
                Yii::app()->db->createCommand("Update rodzaj_konta set aktywny = NOT COALESCE(aktywny,'f') WHERE id_rodzaj_konta = ".$_GET['id'])->execute();
            }
        }
        
        $this->redirect(array('admin/accountlist'));
        
    }
    
    public function actionCreateWorker() {
        $model = new WorkerForm('add');
        
        if(isset($_POST['WorkerForm']))
        {
            $model->attributes = $_POST['WorkerForm'];
            
            if($model->validate()){
                if($model->addWorker() !== 1){
                    $model->addError('firstname','Nie udało się dodać pracownika.');
                }else{
                    $this->redirect(array('admin/info'));
                }
            }
        }
        
        $this->render('worker', array('model' => $model));
    }
    
    public function actionEditWorker() {
        $model = new WorkerForm();
        $worker = Yii::app()->db->createCommand()->select('*')->from('pracownicy p')->where('id_pracownika = '.$_GET['id'])->queryRow();
        $model->attributes = $worker;
        $model->firstname = $worker['imie'];
        $model->lastname = $worker['nazwisko'];
        if(isset($_POST['WorkerForm']))
        {
            $model->attributes = $_POST['WorkerForm'];
            if($model->validate()){
                $editWorker = $model->editWorker($_GET['id']);
                var_dump($editWorker);
                if($editWorker == 0){
                    $model->addError('firstname','Nie udało się edytować pracownika.');
                }else{
                    $this->redirect(array('admin/info'));
                }
            }
        }
        
        $this->render('worker', array('model' => $model));
    }
    
    public function actionWorkerList() {
        $workers = Yii::app()->db->createCommand()->select('*')->from('pracownicy p')->queryAll();
        foreach($workers as $i => $v){
            $workers[$i]['id'] = $v['id_pracownika'];
        }
        $this->render('worker_list', array('workers' => $workers));
    }
    
    public function actionAccountList() {
        $accounts = Yii::app()->db->createCommand()->select('*')->from('rodzaj_konta')->order('id_rodzaj_konta')->queryAll();
        foreach($accounts as $i => $v){
            $accounts[$i]['id'] = $v['id_rodzaj_konta'];
        }
        $this->render('account_list', array('accounts' => $accounts));
    }
    
    public function actionInfo(){
        $this->render('application.views.components.info', array());
    }
    
    
 
}
